var apiHost = wx.getStorageSync("apiHost"); //请求地址
var Appid = wx.getStorageSync('Appid');
var Secret = wx.getStorageSync('Secret');

var AppidFlag = wx.getStorageSync('AppidFlag');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    orderDetails: '',      //订单详情
    cancelModel: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    var orderNo = options.orderNo;
    console.log(orderNo)
    // orderNo = '201809031143949815';

    var token = wx.getStorageSync('token'); //用户的token信息
    that.setData({
      token: token,
      orderNo: orderNo
    })
    that.initOrdersDetail(orderNo, token);  //初始化订单详情
  },

  /**
   * 初始化订单详情
   */
  initOrdersDetail: function (orderNo, token) {
    var that = this;
    wx.showLoading({
      title: '加载中',
      icon: 'loading',
    });

    wx.request({
      url: apiHost + "/mobile/orders/ordersDetail", //仅为示例，并非真实的接口地址
      data: {
        token: token,
        appId: AppidFlag,
        orderNo: orderNo
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        var code = res.data.code;
        var msg = res.data.msg;
        console.log(res)
        if (code == "10000") {
          var orderDetails = res.data.data;
          that.setData({
            orderDetails: orderDetails
          });
        } else {
          that.showMsg(msg);
        }
        wx.hideLoading();   //隐藏加载loading
      }
    })
  },

  /**
   * 调起取消按钮弹窗
   */
  showcCancelorders: function () {
    var that = this;
    that.setData({
      cancelModel: true
    });
  },

  /**
   * 取消订单
   */
  cancelorders: function () {
    var that = this;
    var orderNo = that.data.orderNo;
    var token = that.data.token;
    wx.showLoading({
      title: '加载中',
      icon: 'loading',
    });

    wx.request({
      url: apiHost + "/mobile/orders/cancelorders", //仅为示例，并非真实的接口地址
      data: {
        orderNo: orderNo,
        token: token,
        appId: AppidFlag
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        var code = res.data.code;
        var msg = res.data.msg;

        if (code == "10000") {
          that.initOrdersDetail(orderNo, token);  //初始化订单详情
        } else {
          that.showMsg(msg);
        }
        wx.hideLoading();   //隐藏加载loading
      }
    })
  },

  /**
   * 提醒发货
   */
  remindsend: function () {
    var that = this;
    wx.showLoading({
      title: '加载中',
      icon: 'loading',
    });

    wx.request({
      url: apiHost + "/mobile/orders/remindsend", //仅为示例，并非真实的接口地址
      data: {
        token: token,
        appId: AppidFlag,
        orderNo: orderNo,
        skuId: skuId
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        var code = res.data.code;
        var msg = res.data.msg;

        if (code == "10000") {
          var orderDetails = res.data.data;
          that.setData({
            orderDetails: orderDetails
          });
        } else {
          that.showMsg(msg);
        }
        wx.hideLoading();   //隐藏加载loading
      }
    })
  },

  /**
   * 跳转查看物流
   */
  goLogistics: function () {
    var that = this;
    that.toPage("../logistics/logistics")
  },

  /**
   * 弹窗取消按钮 
   */
  cancel: function () {
    this.setData({
      cancelModel: false
    })
  },

  //跳转友惠特卖
  toPage: function (url) {
    // 加载中
    wx.showLoading({
      title: '加载中',
      icon: 'loading',
    });

    wx.navigateTo({
      url: url,
      success: function () {
        wx.hideLoading();   //隐藏加载loading
      },
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  /**
 * 提示信息
 */
  showMsg: function (msg) {
    wx.showToast({
      title: msg,
      icon: 'none',
      duration: 2000,
      mask: true
    });
  },
})